package com.example.controller;

import com.example.common.Result;
import com.example.entity.Activity;
import com.example.service.ActivityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @GetMapping("/selectAll")
    public Result selectAll(Activity activity){
        List<Activity> activityslist=activityService.selectAll(activity);
        return Result.success(activityslist);
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Activity activity){
        PageInfo<Activity> activityPageInfo = activityService.selectPage(pageNum, pageSize,activity);
        return Result.success(activityPageInfo);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Activity activity){
        activityService.add(activity);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Activity activity){
        activityService.update(activity);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        activityService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestParam String ids){
        // 将逗号分隔的字符串转换为 List<Integer>
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        activityService.deleteBatch(idList);
        return Result.success();
    }

    @GetMapping("/selectById/{activityId}")
    public Result selectById(@PathVariable Integer activityId){
        Activity activity=activityService.selectById(activityId);
        return Result.success(activity);
    }
}
